$.extend({
    jajax: function(obj) {
        var url, params, callback, errorHandler, type, async, dataType;
        url = obj.url;
        params = obj.params;
        callback = obj.callback;
        errorHandler = obj.errorHandler;
        type = obj.type;
        async = obj.async;
        dataType = obj.dataType;

        if (!url) throw new Error("url is undefined");
        type = type || "POST";
        async = async || true;
        dataType = dataType || "json";
        $.ajax({
            type: type,
            async: async,
            data: params,
            dataType: dataType,
            url: url,
            //contentType: "application/json;charset=utf-8",
            beforeSend: function(XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("isajax", "true");
                //$(".return_confrim").html("<img src='jquery/images/loading.gif'/>&nbsp;&nbsp;&nbsp;&nbsp;正在验证账户...");
            },
            success: function(d) {
                callback && callback(d);
            },
            error: function(e) {
                if (errorHandler) {
                    errorHandler(e);
                } else {
                    if (!e.responseText) {
                        $.loadend();
                        return;
                    }

                    if (JSON.parse(e.responseText).errorMessage) {
                        $.jAlert(JSON.parse(e.responseText).errorMessage);
                    } else {
                        $.jAlert(e.responseText);
                    }
                    $.loadend();
                }
            }
        });
    },
    jalert: function(message) {
        alert(message);
    },
    submitForm: function(url, obj, options) {
        ///<summary>表单提交</summary>
        ///<param name=""></param>
        var defalut = { url: url, method: "post", target: "_self" };
        $.extend(true, defalut, options);
        var form = $("<form/>").attr("method", defalut.method).attr("action", defalut.url).attr("target", defalut.target).attr("autocomplete", "off").attr("onsubmit", "javascript:return true");
        $(document.body).append(form);
        for (var x in obj) {
            var input = $('<input type="hidden"/>').attr("name", x).val(obj[x]);
            form.append(input);
        }
        form.submit();
        //form.remove();
    },
    QueryString: function(name) {
        var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
        if (result == null || result.length < 1) {
            return "";
        }
        return result[1];
    }
});


//#region 弹出框
$.extend({
    StockWindow: function (obj) {
        var title, id, url, content, classes, width, callback;
        title = obj.title;
        id = obj.id;
        url = obj.url;
        content = obj.content;
        classes = obj.classes;
        width = obj.width;
        callback = obj.callback;

        var $id = $("#" + id);
        if ($id.size() > 0) {
            return;
        }

        var html = [];
        html.push('<div class="stock-window ' + classes + '" id="' + id + '" style="' + (width ? "width:" + width + "px" : "") + '">');
        html.push('<dl>');
        html.push('<dt>' + title + '<span id="stock-window-close"></span></dt>');
        html.push('<dd>');
        if (url) {
            html.push('<iframe src=""></iframe>');
        } else if (content) {
            html.push(content);
        } else {

        }
        html.push('</dd>');
        html.push('</dl>');
        html.push('</div>');
        $("body").append('<div class="mask-bg"></div>');
        $("body").append(html.join(""));


        $id = $("#" + id);

        $("#" + id).find("dt>span#stock-window-close").bind("click", function () {
            $(".mask-bg").remove();
            $("#" + id).remove();
        });

        $(window).resize(function () {
            $id.css({
                left: ($(window).width() - $id.outerWidth()) / 2,
                top: ($(window).height() - $id.outerHeight()) / 2 + $(document).scrollTop()
            });
        });

        $(window).resize();

        $(window).scroll(function () {
            $id.css({
                left: ($(window).width() - $id.outerWidth()) / 2,
                top: ($(window).height() - $id.outerHeight()) / 2 + $(document).scrollTop()
            });
        });

        if (callback) {
            callback();
        }
        return $id;
    }
});
$.fn.CloseWindow = function () {
    $(this).remove();
    $(".mask-bg").remove();
};

//#endregion